/**
 * 
 */
package com.xbang.attendance.web.action.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.service.BaseService;
import com.xbang.core.struts.support.BaseActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class CommonAction extends BaseActionSupport {

	private static final long serialVersionUID = -2604599843669285127L;

	private static final Log logger = LogFactory.getLog(CommonAction.class);

	private boolean result;

	private String sqlSentence, key, entity;

	private Object value;

	@Autowired
	@Qualifier("baseService")
	private BaseService baseService;

	@Action("/common/delete/normal")
	public String doDeleteJdbc() throws Exception {

		String sqlSentence = "DELETE FROM " + entity + " WHERE " + key + "= :key";

		logger.info(">>>>>>> do delete sql sentence is : " + sqlSentence);
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (value instanceof String[]) {
			value = ((String[]) value)[0];
			if (key.toUpperCase().equals("ID") && value.toString().length() > 0) {
				value = Integer.valueOf(value.toString());
			}
		}
		parameters.put("key", value);
		
		try {
			baseService.execute(sqlSentence, parameters);
			result = true;
		} catch (Exception e) {
			result = false;

		}
		return "j:result";
	}

	@Action("/common/delete/object")
	public String doDeleteObject() throws Exception {

		String hqlSentence = "FROM " + entity + " WHERE " + key + "= :key";
		logger.info(">>>>>>> do delete hql sentence is : " + hqlSentence);
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (value instanceof String[]) {
			value = ((String[]) value)[0];
			if (key.toUpperCase().equals("ID") && value.toString().length() > 0) {
				value = Integer.valueOf(value.toString());
			}
		}
		parameters.put("key", value);

		List lists = baseService.searchForList(hqlSentence, parameters);

		if (lists == null || lists.isEmpty()) {
			result = false;
		} else {
			try {
				baseService.delete(lists.get(0));
				result = true;
			} catch (Exception e) {
				result = false;

			}
		}

		return "j:result";
	}

	@Action("/common/duplicate")
	public String duplicate() throws Exception {

		if (value instanceof String[]) {
			value = ((String[]) value)[0];
		}
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put(key, value);
		logger.info(">>>>>>>>>>>>>>>>>>>>" + sqlSentence);
		int iCount = baseService.queryForInt(sqlSentence, parameters);

		result = iCount > 0;
		return "j:result";
	}

	/**
	 * @return the result
	 */
	public boolean isResult() {
		return result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(boolean result) {
		this.result = result;
	}

	/**
	 * @return the sqlSentence
	 */
	public String getSqlSentence() {
		return sqlSentence;
	}

	/**
	 * @param sqlSentence
	 *            the sqlSentence to set
	 */
	public void setSqlSentence(String sqlSentence) {
		this.sqlSentence = sqlSentence;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @return the value
	 */
	public Object getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(Object value) {
		this.value = value;
	}

	/**
	 * @return the entity
	 */
	public String getEntity() {
		return entity;
	}

	/**
	 * @param entity
	 *            the entity to set
	 */
	public void setEntity(String entity) {
		this.entity = entity;
	}

}
